package cn.virens.common.mybatis.mapper.example.util;

import cn.virens.common.util.exception.APIException;

import java.util.Collection;
import java.util.Map;

public class Util {
    public static final String OR = "OR ";
    public static final String AND = "AND ";
    public static final String TRIM = "AND |OR ";

    /**
     * 判断不为null
     *
     * @param object 值
     * @return 无效返回值
     * @throws APIException 条件空异常
     */
    public static String notNull(Object object) throws APIException {
        if (object == null || isEmpty(object)) {
            throw new APIException("条件空");
        } else {
            return "";
        }
    }

    /**
     * 判断是否为空
     *
     * @param value 值
     * @return 是否为空
     * @throws APIException 一般异常
     */
    public static boolean isEmpty(Object value) throws APIException {
        if (value != null && value.getClass().isArray()) {
            return ((Object[]) value).length <= 0;
        } else if (value instanceof CharSequence) {
            return ((CharSequence) value).length() <= 0;
        } else if (value instanceof Collection) {
            return ((Collection<?>) value).isEmpty();
        } else if (value instanceof Map) {
            return ((Map<?, ?>) value).isEmpty();
        } else {
            return value == null;
        }
    }

    /**
     * 判断是否不为空
     *
     * @param value 值
     * @return 是否不为空
     * @throws APIException 一般异常
     */
    public static boolean isNotEmpty(Object value) throws APIException {
        return value != null && !isEmpty(value);
    }
}
